<?php
/**
 * Created by PhpStorm.
 * User: dung
 * Date: 11/11/14
 * Time: 10:51 AM
 */

class Blog extends Eloquent
{

    use SoftDeletingTrait;

    protected $dates = ['deleted_at'];

    public function BCategory()
    {
        return $this->belongsTo('Bcategory', 'ncategory_id');
    }

    public function blogHomeContest()
    {
        $key = 'Blog-Home-Contest';
        $blogs = array();
        if (Cache::has($key))
            $blogs = Cache::get($key);
        else {
            $blogs = $this->where('ncategory_id', '=', Config::get('constants.CATEGORY_NEWS_EVENT'))->take(6)->get();
            Cache::put($key, $blogs, 30);
        }
        return $blogs;
    }

    public function saveItem($arrParams, $options = null)
    {
        $row = empty($arrParams['id']) ? new Blog() : $this->find($arrParams['id']);
        foreach ($arrParams as $key => $value) {
            $row->{$key} = $value;
        }
        $row->save();
        return $row->id;
    }
} 